package com.sams.controller;

import com.alibaba.fastjson.JSON;
import com.sams.pojo.CLASS;
import com.sams.pojo.Notice;
import com.sams.service.Class_Service;
import com.sams.service.NoticeService;
import com.sams.util.CommonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addNotice")
public class AddNotice extends HttpServlet {
    private NoticeService noticeService = new NoticeService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        Notice notice = JSON.parseObject(params, Notice.class);
        System.out.println(notice.toString());
        HttpSession session = request.getSession();
        String userid = (String)session.getAttribute("userid");
        String user_name = (String)session.getAttribute("user_name");
        notice.setUser_id(userid);
        notice.setUser_name(user_name);
        notice.setTime(CommonUtils.getTimes());
        boolean addData = noticeService.addData(notice);
        if (addData){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
